home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / userdef.zip / USERDEF.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  7KB  |  263 lines

  1. /********************************************************************
  2. **                                                                 **
  3. **  UserDef.exe - Define user environment via the HardWare address **
  4. **                of the network card (WesternDigital)             **
  5. **                                                                 **
  6. **  Version 1.0; 3/25/91 millsm                                    **
  7. **      - First version.  Looks up hardware addresses for WD cards **
  8. **      - Reads in environment variable from text file and sets    **
  9. **        the parent environment variable table.                   **
  10. **  Version 1.01; 3/29/91 millsm                                   **
  11. **      - Added a GLOBAL address that contains settings for ALL    **
  12. **        addresses.                                               **
  13. **  Version 1.02; 6/10/91 millsm                                   **
  14. **      - Remove environment variable with the same name.          **
  15. **      - Add a DOS command in the parsing.                        **
  16. **                                                                 **
  17. *********************************************************************
  18. ** Written with Borland C++                                        **
  19. ********************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <dos.h>
  23.  
  24. typedef char byte;
  25. typedef short int word;
  26.  
  27. typedef struct {                        // This structure describes
  28.     word    psp_int20;              // some of the more interesting
  29.     word    psp_top;                // parts of the PSP
  30.     byte    psp_reserv1[6];
  31.     word    psp_termIP;
  32.     word    psp_termCS;
  33.     word    psp_ctlbrkIP;
  34.     word    psp_ctlbrkCS;
  35.     word    psp_critIP;
  36.     word    psp_critCS;
  37.     word    psp_parentpsp;
  38.     byte    psp_reserv2[20];
  39.     word    psp_environ;
  40.     byte    psp_reserv3[34];
  41.     word    psp_dos;
  42.     byte    psp_reserv4[10];
  43.     byte    psp_fcb1[16];
  44.     byte    psp_fcb2[20];
  45.     byte    psp_parmlen;
  46.     byte    psp_parms[127];
  47.     } psp, far *pspPtr;
  48.  
  49.  
  50. char ProgTitle[]="UserDef Ver 1.02 - 1991 - Matthew Mills";
  51.  
  52. char HWADDR[20];                        // Hardware address string
  53. char Hex[] = "0123456789ABCDEF";        // Hex conversion array
  54. char instring[255];                     // Input file string
  55. FILE *datFile;                          // File Handle to Definition File
  56. int  verboseflag;                       // How noisy the output should be
  57.  
  58.  
  59. #define OFFSET_ADDR 0x08
  60. #define WD1 0x00
  61. #define WD2 0x00
  62. #define WD3 0xc0
  63.  
  64.  
  65. void initialize( s )
  66. char *s;
  67. {
  68.  
  69.     datFile = fopen( s , "r" );
  70.     if ( datFile == NULL ) {
  71.         printf("Unable to open data file '%s'.\07\n", s );
  72.         exit( 1 );
  73.     }
  74. }
  75.  
  76.  
  77. void GetHWAddr()
  78. {
  79. unsigned char addr[6];
  80. char *s;
  81. int i;
  82. int m,port;
  83.  
  84.     s = HWADDR;
  85.     m = 0x200;
  86.     while ( m <= 0x380 ) {
  87.         port = m + OFFSET_ADDR;
  88.         for ( i=0 ; i < 6 ; i++ ) addr[i] = inportb( port++ );
  89.         if (( addr[0] == WD1) &&
  90.             ( addr[1] == WD2 ) &&
  91.             ( addr[2] == WD3 )) {
  92.             for ( i=0 ; i < 6 ; i++ ) {
  93.             *s++ = Hex[( addr[i] & 0xf0 ) >> 4 ];
  94.             *s++ = Hex[ addr[i] & 0x0f ];
  95.             if (i < 5) *s++ = ':';
  96.             }
  97.             *s++ = 0;
  98.             return;
  99.         }
  100.         m = m + 0x020;
  101.     }
  102.     strcpy( "NONE:" , s);
  103. }
  104.  
  105.  
  106.  
  107. char far *getParentEnv()
  108. {
  109. pspPtr temp;
  110.  
  111.     temp = MK_FP(_psp,0x00);
  112.     temp =(pspPtr) MK_FP(temp->psp_parentpsp,0x00); // Offset to parents psp
  113.     return((char far *) MK_FP(temp->psp_environ, 0x00)); // Offset to Environment address
  114. }
  115.  
  116.  
  117. void setParentEnv( s )
  118. char *s;
  119. {
  120. char far *env,far *e1,far *e2;
  121. char *s1;
  122.  
  123.     env = getParentEnv();           // Get parent Environment space
  124.  
  125.     e1 = env;
  126.     while( *e1 ) {
  127.         e2 = e1;
  128.         s1 = s;
  129.         while( *e2 ){
  130.             if( *e2 != *s1 ) {      // this is a unique variable
  131.                 while( *e1 ) *env++ = *e1++; // copy this var
  132.                 *env++ = 0;     // add the null
  133.                 e1++;           // skip passed the null
  134.                 break;          // end the comparison
  135.             }
  136.             if( *s1 == '=' ) {      // we matched a variable
  137.                 while( *e2++ ); // find the end of the var
  138.                 e1 = e2;        // skip the entire entry
  139.                 break;          // leave our comparison loop
  140.             }
  141.             s1++; e2++;             // set to the next characters
  142.         }
  143.     }  // We should be at the end of our environment space
  144.  
  145.     while( *s ) *env++ = *s++;      // copy passed string into space
  146.     *env++ = 0; *env = 0;           // make it a double null
  147. }
  148.  
  149.  
  150.  
  151. void parsefile()
  152. {
  153. char *s,c;
  154. int  flag;              // reading flag
  155. int  found = 0;         // Found HWADDR in table flag
  156. int  parse = 0;         // Parse Flag
  157.  
  158. while(1) {
  159.     s = instring;
  160.     flag = 1;
  161.     while( flag ) {
  162.         c = fgetc( datFile );
  163.         switch( c ){
  164.         case '\n':
  165.             *s = 0;
  166.             flag = 0;
  167.             break;
  168.         case EOF:
  169.             return;
  170.         case ' ':
  171.             if (flag == 2) *s++ = c;
  172.             break;
  173.         case ';':
  174.             while( fgetc( datFile ) != '\n' );
  175.             *s = 0;
  176.             flag = 0;
  177.             break;
  178.         default:
  179.             *s++ = c;
  180.             flag = 2;
  181.         }
  182.     }
  183.     if (verboseflag == 2) printf("%s\n", instring);
  184.     if (parse) {
  185.         if ( instring[0] == ':' ) parse = 0;    // End of definition
  186.                             // is the start of the
  187.                             // next definition
  188.  
  189.         if ((verboseflag == 1) && parse) printf("%s\n",instring);
  190.  
  191.         if ( strncmp( &instring[0] , "set " , 4) == 0 ) {
  192.             setParentEnv( &instring[4] );   // Set Env.Variable
  193.         }
  194.         if ( strncmp( &instring[0] , "dos " , 4) == 0 ) {
  195.             system( &instring[4] );         // Issue Dos command
  196.         }
  197.         if ( strncmp( &instring[0] , "echo ", 5) == 0 ) {
  198.             puts( &instring[5] );           // Echo a message
  199.         }
  200.     }
  201.     if (!parse) {
  202.         if ( strncmp( &instring[0] , ":GLOBALS" , 8) == 0 ) parse = 1;
  203.         if(( strncmp( &instring[0] , ":DEFAULT" , 8) == 0 ) && !(found))
  204.             parse = 1;
  205.         if ( strncmp( &instring[1] , HWADDR ,
  206.             strlen( HWADDR) ) == 0 ){
  207.             parse = 1;
  208.             found = 1;
  209.             }
  210.     }
  211.    }
  212. }
  213.  
  214.  
  215.  
  216. void main( argc, argv )
  217. int argc;
  218. char *argv[];
  219. {
  220. int     i;
  221. char    *s;
  222. int     printhelp = 0;                 // Flag Modifiers
  223. int     doitflag = 1;
  224. char    datafilename[255] = "userdef.dat";  // Default data file
  225.  
  226.     verboseflag = 0;
  227.     i = 1;
  228.     while( argc > i ){
  229.         s = argv[i];
  230.         if (s[0] == '/') {
  231.             if ((s[1] == '?') || (s[1] == 'h')){
  232.                 printhelp = 1;
  233.                 doitflag = 0;
  234.             }
  235.             if (s[1] == 'v') verboseflag = 1;
  236.             if (s[1] == 'd') verboseflag = 2; // debug level
  237.         }
  238.         else {
  239.             strcpy( datafilename , s );   // passed data file
  240.         }
  241.         i++;
  242.     }
  243.  
  244. if (doitflag) {
  245.     if (verboseflag) printf("Using file:%s\n",&datafilename);
  246.     initialize( &datafilename );
  247.     GetHWAddr();
  248.     if (verboseflag) printf("My Hardware address is:%s\n",HWADDR);
  249.     parsefile();
  250.     }
  251. if (printhelp) {
  252.     printf( "%s\n",ProgTitle);
  253.     printf( "USERDEF <filename> </<h,?,v>>\n");
  254.     printf( "       : filename = definition table path\n");
  255.     printf( "             defaults to USERDEF.DAT\n");
  256.     printf( "       : /h,/?    = Help info (this screen)\n");
  257.     printf( "       : /v       = Verbose mode\n");
  258.     }
  259.  
  260.     exit(0);
  261. }
  262.                 
  263.